package com.srwl.mytx.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.srwl.mytx.Constant;
import com.srwl.mytx.EaseConstant;
import com.srwl.mytx.domain.Contact;
import com.srwl.mytx.domain.User;
import com.srwl.mytx.R;
import com.srwl.mytx.activity.ChatActivity;
import com.srwl.mytx.utils.GlideLoadUtils;

import java.util.List;
import java.util.Map;

@Deprecated
public class ExpandableAdapter extends BaseExpandableListAdapter {
    private static final String TAG = "ExpandableAdapter";
    private List<String> parentList;
    private Map<String, List<Contact>> childMap;
    private Context context;
    private ItemClickListener itemClickListener;


    public ExpandableAdapter(Context context, List<String> parentList, Map<String, List<Contact>> childMap) {
        this.context = context;
        this.parentList = parentList;
        this.childMap = childMap;
    }

    //获取分组数
    @Override
    public int getGroupCount() {
        return parentList.size();
    }

    //获取当前组的子项数
    @Override
    public int getChildrenCount(int groupPosition) {
        String groupName = parentList.get(groupPosition);
        int childCount = childMap.get(groupName).size();
        return childCount;
    }

    //获取当前组对象
    @Override
    public Object getGroup(int groupPosition) {
        String groupName = parentList.get(groupPosition);
        return groupName;
    }

    //获取当前子项对象
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        String groupName = parentList.get(groupPosition);
        return childMap.get(groupName).get(childPosition);

    }

    //获取组ID
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    //获取子项ID
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    //组视图初始化
    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        final int groupPos = groupPosition;
        if (convertView == null) {
//
            convertView = LayoutInflater.from(context).inflate(R.layout.tx_expandablelist_item_parent, null);
        }
        ImageButton btn_theMoreForParent = (ImageButton) convertView.findViewById(R.id.btn_theMore);

        btn_theMoreForParent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//
//                final BottomDialogOld parentDialog = BottomDialogOld.newInstance(parentDialogItems, BottomDialogOld.AREA_FLAG_BOTTOM);
//                parentDialog.show(getFragmentManager(), "ParentDialog");
//                parentDialog.setOnItemClickListener(new BottomDialogOld.InteractionListener() {
//                    @Override
//                    public void onDismiss() {
//
//                    }
//
//                    @Override
//                    public void onClick(final int position, String itemType) {
//                        clickEvent(position, groupPosition);
//                    }
//                });
            }
        });
        TextView tv_parent = (TextView) convertView.findViewById(R.id.tv_name);
        tv_parent.setText(parentList.get(groupPosition));
        return convertView;
    }


    //子项视图初始化
    @Override
    public View getChildView(final int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {

            convertView = LayoutInflater.from(context).inflate(R.layout.tx_expandableitem_child, parent, false);

        }
        final Contact contact = (Contact) getChild(groupPosition, childPosition);
        String status = contact.getStatus();
        if (status != null && status.equals(Constant.CONTACT_STATUS_ACTIVE_DELETE)) {
            convertView.setBackgroundColor(context.getResources().getColor(R.color.gray_background));
        } else {
            convertView.setBackgroundColor(context.getResources().getColor(R.color.transparent));
        }
        final User targetUser = contact.getTargetUser();
        TextView tv_name = convertView.findViewById(R.id.tv_name);
        final ImageView img_icon = convertView.findViewById(R.id.iv_avatar);
        if (targetUser != null) {
            if (!TextUtils.isEmpty(targetUser.getUsername())) {
                tv_name.setText(targetUser.getUsername());
            } else {
                tv_name.setText(targetUser.getPhone());
            }

            GlideLoadUtils.glideLoad(context, targetUser.getAvatar(), img_icon, R.drawable.tx_default_avatar_1);
        } else {
            Log.e(TAG, "getChildView: targetUser是空的");
        }

        //Glide.with(context).load(targetUser.getAvatar()).diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.drawable.tx_default_avatar).into(img_icon);

        View rl_theMore = convertView.findViewById(R.id.rl_the_more);
        final View moreView = convertView.findViewById(R.id.iv_theMore);
        // rl_theMore.setFocusable(false);
        rl_theMore.setOnClickListener(new View.OnClickListener() {
                                          @Override
                                          public void onClick(View v) {
//                                              ViewAnimator.animate(moreView).rotation(90).duration(300).onStop(new AnimationListener.Stop() {
//                                                  @Override
//                                                  public void onStop() {
//                                                      moreView.setBackground(ResourcesCompat.getDrawable(context.getResources(), R.drawable.tx_more_arrow_blue, null));
//                                                  }
//                                              }).start();

                                              if (itemClickListener != null) {
                                                  itemClickListener.childItemShowDialogClick(contact);
                                              }

                                          }

                                      }
        );

        convertView
                .findViewById(R.id.rl_to_chat)
                .setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {

                                            Intent intent = new Intent(context, ChatActivity.class).putExtra(EaseConstant.EXTRA_TARGET_USER_INFO, targetUser);
                                            if (contact.getStatus().equals(Constant.CONTACT_STATUS_ACTIVE_DELETE)) {
                                                intent.putExtra("CONTACT_STATUS", Constant.CONTACT_STATUS_ACTIVE_DELETE);
                                            }

                                            context.startActivity(intent);
                                        }
                                    }
                );
        return convertView;

    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface ItemClickListener {
        void groupItemClick();

        void childItemClick();

        void childItemShowDialogClick(Contact contact);
    }
}
